package com.study.gulimall.ware;

import java.util.concurrent.TimeUnit;

/**
 * @author tianxinyu
 * @create 2021-06-20 9:55
 */
public class VolatileDemo {
    public static void main(String[] args) {
        myData myData = new myData();
        for (int i = 1; i <=20 ; i++) {
            new Thread(()->{
                for (int j = 1; j <=1000 ; j++) {
                    myData.addSelf();
                }
            },String.valueOf(i)).start();
        }
        try {
            TimeUnit.SECONDS.sleep(4);
        }catch (Exception e){
            e.printStackTrace();
        }
        while (Thread.activeCount()>2){
            Thread.yield();

        }

        System.out.println(Thread.currentThread().getName()+"\t finally num value is "+myData.num);
    }


}
class myData{
    volatile  int num = 0;
    public void addToSixty() {
       num = 60;
    }
    public   void addSelf(){
        num++;
    }
}